import 'package:flutter/material.dart';
import 'ErrorProblemsPage.dart';
import 'FractionPracticePage.dart';
import 'PrecisionPractice.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Math Quiz App',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 年级列表
  final List<String> grades = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'];
  String selectedGrade = '一年级'; // 默认选中的年级

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFE6F0FF),
      appBar: AppBar(
        title: const Text('小孩天天练', style: TextStyle(color: Color(0xFFa6fff2))),
        backgroundColor: const Color(0xFF5e63b6),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildCalendarSection(),
              const SizedBox(height: 10),
              _buildGradeSelection(), // 年级选择部分
              const SizedBox(height: 10),
              _buildDailyTaskSection(context),
              const SizedBox(height: 10),
              _buildImageSection(context), // 传入 context
              const SizedBox(height: 10),
              _buildBottomIcons(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildCalendarSection() {
    // 获取当前日期
    final DateTime today = DateTime.now();
    // 当前年月
    final String currentMonthYear = "${today.year}年${today.month}月";
    // 生成未来7天的日期
    final List<DateTime> weekDates = List.generate(
      7,
          (index) => today.add(Duration(days: index)),
    );
    // 星期映射
    const List<String> weekdays = ['日', '一', '二', '三', '四', '五', '六'];

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          children: [
            Icon(Icons.calendar_today, color: Colors.orange),
            SizedBox(width: 8),
            Text(
              '学习进度更新啦，答对1题+1积分!',
              style: TextStyle(fontSize: 16, color: Colors.blueAccent),
            ),
          ],
        ),
        const SizedBox(height: 10),
        // 显示当前年月
        Text(
          currentMonthYear,
          style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: List.generate(7, (index) {
            final DateTime date = weekDates[index];
            final bool isToday = index == 0;

            return Column(
              children: [
                CircleAvatar(
                  backgroundColor: isToday ? Colors.purple : Colors.grey,
                  child: Text(
                    isToday ? '今' : '${date.day}',
                    style: const TextStyle(color: Colors.white),
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  weekdays[date.weekday % 7], // 映射为星期
                  style: TextStyle(
                    fontSize: 14,
                    color: isToday ? Colors.purple : Colors.black,
                  ),
                ),
              ],
            );
          }),
        ),
      ],
    );
  }

  Widget _buildDailyTaskSection(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          children: [
            Text(
              '今日任务',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            Spacer(),
            Text(
              '去完成 +20',
              style: TextStyle(fontSize: 16, color: Colors.orange),
            ),
          ],
        ),
        const SizedBox(height: 10),
        Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.green.shade100,
            borderRadius: BorderRadius.circular(16),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.4),
                spreadRadius: 2,
                blurRadius: 5,
                offset: const Offset(0, 3),
              ),
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Row(
                children: [
                  Text(
                    '数学',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: Colors.blue,
                    ),
                  ),
                  SizedBox(width: 20),
                  Text(
                    '语文',
                    style: TextStyle(
                      fontSize: 18,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 10),
              const Text('加、减、乘、除、混合运算练习', style: TextStyle(fontSize: 16)),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => FractionPracticePage(grade: selectedGrade),
                    ),
                  );
                },
                style: ElevatedButton.styleFrom(backgroundColor: Colors.lightBlueAccent),
                child: const Text('继续练习', style: TextStyle(color: Color(0XFFebfffa))),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildGradeSelection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '选择年级',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 5),
          decoration: BoxDecoration(
            color: Colors.yellow.shade100,
            borderRadius: BorderRadius.circular(16),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.5),
                spreadRadius: 2,
                blurRadius: 5,
                offset: const Offset(0, 3),
              ),
            ],
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text(
                '当前年级:',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              DropdownButton<String>(
                value: selectedGrade,
                underline: Container(), // 去掉下划线
                borderRadius: BorderRadius.circular(12),
                onChanged: (String? newValue) {
                  setState(() {
                    selectedGrade = newValue!;
                  });
                },
                items: grades.map<DropdownMenuItem<String>>((String grade) {
                  return DropdownMenuItem<String>(
                    alignment: Alignment.center,
                    value: grade,
                    child: Text(
                      grade,
                      style: const TextStyle(fontSize: 16),
                    ),
                  );
                }).toList(),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildImageSection(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        _buildImageCard(
          icon: Icons.close,
          title: '收集错题',
          subtitle: '智能错题本',
          backgroundColor: Colors.lightBlueAccent,
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => ErrorProblemsPage()),
            );
          },
        ),
        _buildImageCard(
          icon: Icons.auto_fix_high,
          title: '精准练习',
          subtitle: '快速查缺补漏',
          backgroundColor: Colors.purpleAccent,
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => ExamPage()), // 替换为实际的成功页面
            );

          },
        ),
      ],
    );
  }

  Widget _buildImageCard({
    required IconData icon,
    required String title,
    required String subtitle,
    required Color backgroundColor,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 150,
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: backgroundColor.withOpacity(0.2),
          borderRadius: BorderRadius.circular(16),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Icon(icon, size: 40, color: Colors.white),
            const SizedBox(height: 10),
            Text(title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            const SizedBox(height: 5),
            Text(subtitle, style: const TextStyle(fontSize: 14, color: Colors.black54)),
          ],
        ),
      ),
    );
  }

  Widget _buildBottomIcons() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        _buildBottomIcon(Icons.assignment, '错题本', iconColor: Colors.red),
        _buildBottomIcon(Icons.bar_chart, '学习榜', iconColor: Colors.blue),
        _buildBottomIcon(Icons.card_giftcard, '积分奖励', iconColor: Colors.green),
        _buildBottomIcon(Icons.assessment, '学习报告', iconColor: Colors.orange),
      ],
    );
  }

  Widget _buildBottomIcon(IconData icon, String label, {Color iconColor = Colors.orange}) {
    return Column(
      children: [
        Icon(icon, size: 40, color: iconColor),
        const SizedBox(height: 5),
        Text(label, style: const TextStyle(fontSize: 14)),
      ],
    );
  }
}



